<template>
  <div class="notification-list">
    <el-card class="notification-card">
      <template #header>
        <div class="card-header">
          <span>通知列表</span>
          <div class="header-actions">
            <el-badge :value="unreadCount" :max="99" v-if="unreadCount > 0">
              <el-button type="primary" @click="refreshList">刷新</el-button>
            </el-badge>
            <el-button v-else type="primary" @click="refreshList">刷新</el-button>
          </div>
        </div>
      </template>

      <el-table
        :data="notificationList"
        v-loading="loading"
        style="width: 100%"
        row-key="id"
      >
        <el-table-column prop="id" label="ID" width="80" />
        <el-table-column prop="title" label="标题" show-overflow-tooltip>
          <template #default="scope">
            <el-tag
              :type="getNotificationTypeTag(scope.row.type)"
              effect="plain"
            >
              {{ formatNotificationType(scope.row.type) }}
            </el-tag>
            <span :class="{ 'unread': !scope.row.isRead }">{{ scope.row.title }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="content" label="内容" show-overflow-tooltip width="300" />
        <el-table-column prop="channel" label="渠道" width="100">
          <template #default="scope">
            {{ formatChannel(scope.row.channel) }}
          </template>
        </el-table-column>
        <el-table-column prop="createdAt" label="时间" width="180" />
        <el-table-column label="操作" width="200" fixed="right">
          <template #default="scope">
            <el-button
              size="small"
              type="primary"
              @click="markAsRead(scope.row)"
              :disabled="scope.row.isRead"
              v-if="!scope.row.isRead"
            >
              标为已读
            </el-button>
            <el-button
              size="small"
              @click="viewDetail(scope.row)"
              v-if="scope.row.link"
            >
              查看详情
            </el-button>
            <el-button
              size="small"
              type="danger"
              @click="deleteNotification(scope.row)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <el-pagination
          v-model:current-page="pagination.page"
          v-model:page-size="pagination.pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :total="pagination.total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>

      <div class="batch-actions" style="margin-top: 20px;">
        <el-button
          type="primary"
          @click="markAllAsRead"
          :disabled="unreadCount === 0"
        >
          全部标为已读
        </el-button>
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import type {
  Notification,
  NotificationListResponse,
  UnreadCountResponse
} from '@/types/api/notification'
import { notificationApi } from '@/api/modules/notification'
import { formUtils } from '@/utils'

// 表格相关
const loading = ref(false)
const notificationList = ref<Notification[]>([])
const unreadCount = ref(0)

// 分页相关
const pagination = reactive({
  page: 1,
  pageSize: 10,
  total: 0
})

// 获取通知列表
const getNotificationList = async () => {
  try {
    loading.value = true
    const res = await notificationApi.getNotificationList({
      page: pagination.page,
      pageSize: pagination.pageSize
    })

    const data = res.data.data
    notificationList.value = data.list
    pagination.total = data.total
    unreadCount.value = data.unreadCount
  } catch (error) {
    ElMessage.error('获取通知列表失败')
  } finally {
    loading.value = false
  }
}

// 获取未读数量
const getUnreadCount = async () => {
  try {
    const res = await notificationApi.getUnreadCount()
    unreadCount.value = res.data.data.total
  } catch (error) {
    console.error('获取未读数量失败')
  }
}

// 刷新列表
const refreshList = () => {
  getNotificationList()
  getUnreadCount()
}

// 标为已读
const markAsRead = async (row: Notification) => {
  try {
    await notificationApi.markAsRead(row.id)
    ElMessage.success('标记成功')
    refreshList()
  } catch (error) {
    ElMessage.error('标记失败')
  }
}

// 全部标为已读
const markAllAsRead = async () => {
  try {
    await notificationApi.markAllAsRead()
    ElMessage.success('全部标记为已读')
    refreshList()
  } catch (error) {
    ElMessage.error('操作失败')
  }
}

// 删除通知
const deleteNotification = async (row: Notification) => {
  formUtils.handleDeleteConfirm('这条通知', async () => {
    await notificationApi.deleteNotification(row.id)
    refreshList()
  }, {
    successMessage: '删除成功',
    errorMessage: '删除失败'
  })
}

// 查看详情
const viewDetail = (row: Notification) => {
  if (row.link) {
    // 跳转到详情页面
    window.open(row.link, '_blank')
  }
}

// 分页相关方法
const handleSizeChange = (val: number) => {
  pagination.pageSize = val
  getNotificationList()
}

const handleCurrentChange = (val: number) => {
  pagination.page = val
  getNotificationList()
}

// 格式化通知类型
const formatNotificationType = (type: string) => {
  const typeMap: Record<string, string> = {
    'COURSE': '课程',
    'CARD': '会员卡',
    'SYSTEM': '系统',
    'OTHER': '其他'
  }
  return typeMap[type] || type
}

// 获取通知类型标签
const getNotificationTypeTag = (type: string) => {
  const tagMap: Record<string, string> = {
    'COURSE': 'success',
    'CARD': 'warning',
    'SYSTEM': 'danger',
    'OTHER': 'info'
  }
  return tagMap[type] || 'info'
}

// 格式化渠道
const formatChannel = (channel: string) => {
  const channelMap: Record<string, string> = {
    'INTERNAL': '站内信',
    'SMS': '短信',
    'EMAIL': '邮件',
    'PUSH': '推送'
  }
  return channelMap[channel] || channel
}

// 生命周期
onMounted(() => {
  refreshList()
})
</script>

<style scoped>
.notification-list {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

.unread {
  font-weight: bold;
}
</style>
